import { observable, action, toJS } from 'mobx';
import { Tools, Toast } from 'weaver-mobile';
import { Form } from 'weaver-mobile-page';

import * as API from '../../api/home';
const getLabel = Tools.getLabel;

export default class HomeStore {
  @observable dataChange = false;
  @observable userid = "";
  @observable home = 'something';
  @observable selectPage = {
    get type() { return 'home' },
    options: [],
    selectedKeys: [],
    searchText: "",
    updateSelectedKey: (datas) => {
      this.selectPage.selectedKeys = datas;
    },
    get viewOptions() {
      return this.options.filter(item => item.showname.includes(this.searchText));
    }
  }

  @observable sublistSelectPage = {
    get type() { return 'sub' },
    options: [],
    selectedKeys: [],
    searchText: "",
    updateSelectedKey: (datas) => {
      this.sublistSelectPage.selectedKeys = datas;
    },
    get viewOptions() {
      return this.options.filter(item => item.showname.includes(this.searchText));
    }
  }
  @observable userMsgConfig = [];
  @observable detailSetting = {
    list: {
      subList: []
    },
    index: 0,
    tabIndex: 0,
    title: "",
    syncModal: false,
  }
  @observable detailSettingForm = new Form.Store({ route: '/home/sublist/detailSetting' })

  @action
  getModuleOptions() {
    API.getModuleOptions().then(res => {
      this.selectPage.options = res.conditions[1].options;
    })
  }

  @action
  getLoadUserMsgConfig(params) {
    API.loadUserMsgConfig(params).then(res => {
      this.userMsgConfig = res.datas;
      this.userid = res.userid;
    })
  }

  @action
  saveUserMsgConfig() {
    const params = {
      datas: JSON.stringify(toJS(this.userMsgConfig)),
      userid: this.userid
    }
    Toast.loading("保存中", 1);
    API.saveUserMsgConfig(params).then(res => {
      this.dataChange = false;
      Toast.hide();
      this.getLoadUserMsgConfig();
      this.selectPage.selectedKeys = [];
    })
  }

  @action
  getWorkflowCondition(params) {
    const apiParams = {
      module: params.module,
      userid: this.userid
    }
    // this.detailSettingForm = new Form.Store({ route: '/home/sublist/detailSetting' })
    API.getWorkflowCondition(apiParams).then(res => {
      this.detailSettingForm = new Form.Store({ route: '/home/sublist/detailSetting' })
      this.detailSettingForm.initForm(res.conditions, `${params.route}/detailSetting`);
      // 下面这段数据计算是因为拿到condition初始化以后，form 参数受控有问题，需要重新改写 Field
      const JSONFormParams = JSON.parse(JSON.stringify((this.detailSettingForm.formParams)));
      const selected = JSON.parse(JSON.stringify((this.detailSettingForm.selectLinkages)));
      let updateFields = {};
      Object.keys(JSONFormParams).map(item => {
        let browserData = null;
        if (JSONFormParams[item][item] !== "0") {
          let optionsKey = selected[item].options.filter(item => item.selected)[0].key;
          let rpDatas = selected[item].selectLinkageDatas[optionsKey].browserConditionParam.replaceDatas;
          if (rpDatas) {
            browserData = { [selected[item].selectLinkageDatas[optionsKey].domkey[0]]: [...rpDatas] }
          }
        }
        updateFields[item] = {
          value: {
            [item]: JSONFormParams[item][item],
            ...browserData
          }
        }
      })
      this.detailSettingForm.updateFields(updateFields);
      if (params.callback) {
        params.callback();
      }
    })
  }

  @action
  getDetailCondition(params) {
    const apiParams = {
      id: params.id,
      typeid: params.typeid,
      module: params.module,
      userid: this.userid
    }
    // this.detailSettingForm = new Form.Store({ route: '/home/sublist/detailSetting' })
    API.getDetailCondition(apiParams).then(res => {
      this.detailSettingForm = new Form.Store({ route: '/home/sublist/detailSetting' })
      this.detailSettingForm.initForm(res.conditions, `${params.route}/detailSetting`);
      const JSONFormParams = JSON.parse(JSON.stringify((this.detailSettingForm.formParams)));
      const selected = JSON.parse(JSON.stringify((this.detailSettingForm.selectLinkages)));
      let updateFields = {};

      Object.keys(JSONFormParams).map(item => {
        // console.log(selected, item)
        let browserData = null;
        if (JSONFormParams[item][item] !== "0") {
          let optionsKey = selected[item].options.filter(item => item.selected)[0].key;
          if (selected[item].selectLinkageDatas[optionsKey].value) {
            let optionsValue = selected[item].selectLinkageDatas[optionsKey].value.split(",")[0];
            let rpDatas = selected[item].selectLinkageDatas[optionsKey].options[optionsValue];
            if (rpDatas) {
              browserData = { [selected[item].selectLinkageDatas[optionsKey].domkey[0]]: rpDatas.key }
            }
          } else {
            let rpDatas = selected[item].selectLinkageDatas[optionsKey].browserConditionParam.replaceDatas;
            if (rpDatas) {
              browserData = { [selected[item].selectLinkageDatas[optionsKey].domkey[0]]: rpDatas}
            }
          }
        }
            // console.log(browserData);
        updateFields[item] = {
          value: {
            [item]: JSONFormParams[item][item],
            ...browserData
          }
        }
      })
      // console.log(updateFields)
      this.detailSettingForm.updateFields(updateFields);
      if (params.callback) {
        params.callback();
      }
    })
  }

  @action('save')
  saveWorkflowDetail() {
    let myParams = JSON.parse(JSON.stringify(this.detailSettingForm.formParams));
    // console.log(myParams)
    const resParams = {};
    Object.keys(myParams).map(item => {
      resParams[item] = myParams[item] || "0";
      if (Object.keys(myParams[item]).length > 0) {
        Object.keys(myParams[item]).map(innerItem => {
          if (typeof myParams[item][innerItem] != 'string') {
            resParams[innerItem] = myParams[item][innerItem].map(item => item.id).join();
          } else {
            resParams[innerItem] = myParams[item][innerItem];
          }
        }
        )
      }
    })
    const params = {
      userid: this.userid,
      module: this.detailSetting.otherParams.groupcode,
      datas: JSON.stringify(resParams),
      include: this.detailSettingForm.conditioninfo[0].value,
    }
    API.saveWorkflowDetail(params).then(res => {
      Toast.success(getLabel("22619", "保存成功"), 1);
      this.dataChange = false;
    })
  }

  saveDetailConfig() {
    let myParams = JSON.parse(JSON.stringify(this.detailSettingForm.formParams));
    // console.log(myParams)
    const resParams = {};
    Object.keys(myParams).map(item => {
      resParams[item] = myParams[item] || "0";
      if (Object.keys(myParams[item]).length > 0) {
        Object.keys(myParams[item]).map(innerItem => {
          if (typeof myParams[item][innerItem] != 'string') {
            resParams[innerItem] = myParams[item][innerItem].map(item => item.id).join();
          } else {
            resParams[innerItem] = myParams[item][innerItem];
          }
        }
        )
      }
    })
    const params = {
      userid: this.userid,
      id: this.detailSetting.otherParams.id,
      typeid: this.detailSetting.otherParams.typeid,
      module: this.detailSetting.otherParams.groupcode,
      datas: JSON.stringify(resParams),
    }
    API.saveDetailConfig(params).then(res => {
      Toast.success(getLabel("22619", "保存成功"), 1);
      this.dataChange = false;
    })
  }



}
